package com.yazhou;

import com.alibaba.fastjson.JSON;
import com.baidu.aip.face.AipFace;
import com.yazhou.baidu.util.FaceData;
import com.yazhou.util.Base64Util;
import org.json.JSONObject;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.UUID;

/**
 * 创建人：xiaolu
 * 创建时间：2020/9/22 16:45
 * 描述：
 */
public class Sample {

    //设置APPID/AK/SK
    public static final String APP_ID = "22732905";
    public static final String API_KEY = "kkvW86VojcIWVL8TIxW9LHr5";
    public static final String SECRET_KEY = "h0LP0EdBlf3IKOf4AvybNjBNVRMabi1b";

    /**
     * 百度AI 人脸检测接口的调用
     * 可以根据人脸的参数  做一下功能
     * 人脸方向，几个人脸，人脸在图片的位置 x，y坐标轴
     *
     * @param imagePath
     * @param outFile
     * @throws InterruptedException
     */
    public static void facejiequ(String imagePath, String outFile) throws InterruptedException {
        // 初始化一个AipFace
        AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);

        // 调用接口
        String image = Base64Util.ImgToBase64Byte(imagePath);
        String imageType = "BASE64";
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("max_face_num", "1");
        // 人脸检测
        JSONObject res = client.detect(image, imageType, options);

        System.out.println(res.toString(2));

        //判断几个人脸
        //可以获取人脸是否正着

        FaceData faceData = JSON.parseObject(res.toString(2), FaceData.class);
        FaceData.ResultBean result = faceData.getResult();
        FaceData.ResultBean.FaceListBean faceListBean = result.getFace_list().get(0);
        FaceData.ResultBean.FaceListBean.LocationBean location = faceListBean.getLocation();
        double top = location.getTop();
        double left = location.getLeft();
        int height = location.getHeight();
        int width = location.getWidth();

        File f = new File(imagePath);
        cropImage(f, new Double(left).intValue(), new Double(top).intValue(), width, height, "jpg", outFile);
    }

    /**
     * java 原生图片截取
     *
     * @param x     x坐标
     * @param y     y坐标
     * @param w     宽
     * @param h     长
     * @param sufix 生成图片的后缀 jpg  png
     *              //     * @param file  文件地址
     * @return
     */
    public static boolean cropImage(File f, int x, int y, int w, int h, String sufix, String filename) {
//


        BufferedImage bufferedImage = null;
        boolean write = false;

        //颗粒度为 百度width 0.1
        for (int i = 5; i > 0; i--) {
            try {
                InputStream input = new FileInputStream(f);
                bufferedImage = ImageIO.read(input);
            } catch (IOException e) {
                e.printStackTrace();
            }

            int height = bufferedImage.getHeight();
            int width = bufferedImage.getWidth();


            double num = i * 0.1;
            int x1 = x - new Double(w * num).intValue();
            int y1 = y - new Double(h * num).intValue();
            int w1 = new Double(w * (1 + num * 2)).intValue();
            int h1 = new Double(h * (1 + num * 2)).intValue();

            if (x1 >= 0 && x1 <= width && y1 >= 0 && y1 <= height && 0 < x1 + w1 && x1 + w1 < width && 0 < y1 + h1 && y1 + h1 < height) {
                try {
                    bufferedImage = bufferedImage.getSubimage(x1, y1, w1, h1);
                    File file = new File("D:\\生成后\\" + i + "\\" + filename);
                    if (!file.getParentFile().exists()) {
                        file.getParentFile().mkdir();
                    }
                    write = ImageIO.write(bufferedImage, sufix, file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return write;

    }

    public static void main(String[] args) throws InterruptedException {
        //facejiequ("D:\\123.jpg", "D:\\124.jpg");
//        File file = new File("D:\\12\\");
//        File[] files = file.listFiles();
//        for (File file1 : files) {
//
//        }
        File file = new File("D:\\3.jpg");
        facejiequ(file.getAbsolutePath(), file.getName());

    }


}
